using System;

namespace Glue.Internals
{
    internal class NullableTypeComparer
    {
        public bool AreOneTheNullableVersionOfTheOther(Type t1, Type t2)
        {
            if (NullableRuntimeType.IsNullable(t1))
                return IsUnderlyingType(t1, t2);
            if (NullableRuntimeType.IsNullable(t2))
                return IsUnderlyingType(t2, t1);

            return false;
        }

        public bool AreEqualOrNullableEqual(Type t1, Type t2)
        {
            return t1 == t2 || AreOneTheNullableVersionOfTheOther(t1, t2);
        }

        private bool IsUnderlyingType(Type nullableType, Type underlyingType)
        {
            var nullableruntimeType = new NullableRuntimeType(nullableType);
            return nullableruntimeType.GetUnderlyingType() == underlyingType;
        }
    }
}